package com.pro01.backend.mapper;

import com.pro01.backend.dto.UserPromotionRecordDTO;
import com.pro01.backend.pojo.UserPromotion2;
import org.apache.ibatis.annotations.Mapper;

import com.pro01.backend.dto.PromotionActiveDTO;
import com.pro01.backend.pojo.Promotion2;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserPromotion2Mapper {
    @Select("""
    SELECT COUNT(*) FROM user_promotions2
    WHERE user_id = #{userId} AND torrent_id = #{torrentId} AND promotion_type = #{type}
""")
    boolean hasReceived(@Param("userId") Long userId,
                        @Param("torrentId") Long torrentId,
                        @Param("type") String type);

    @Insert("""
    INSERT INTO user_promotions2 (
        user_id, torrent_id, promotion_type,
        reward_uploaded, reward_downloaded, reward_credit, created_at
    ) VALUES (
        #{userId}, #{torrentId}, #{promotionType},
        #{rewardUploaded}, #{rewardDownloaded}, #{rewardCredit}, #{createdAt}
    )
""")
    int insertReward(UserPromotion2 record);

    // 用户查看自己的促销奖励
    @Select("""
    SELECT
        up.torrent_id,
        t.name AS torrent_name,
        up.promotion_type,
        up.reward_uploaded,
        up.reward_downloaded,
        up.reward_credit,
        up.created_at
    FROM user_promotions2 up
    JOIN torrents t ON up.torrent_id = t.id
    WHERE up.user_id = #{userId}
    ORDER BY up.created_at DESC
    LIMIT #{limit} OFFSET #{offset}
""")
    List<UserPromotionRecordDTO> getUserPromotionRecords(
            @Param("userId") Long userId,
            @Param("offset") int offset,
            @Param("limit") int limit
    );

    @Select("SELECT COUNT(*) FROM user_promotions2 WHERE user_id = #{userId}")
    int countUserPromotionRecords(@Param("userId") Long userId);

}
